const path = require("path");
const webpack = require("webpack");

const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");

const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;

const DIST = "dist";
const DIST_PATH = path.resolve(__dirname, DIST);
const PUBLIC = "";

const isProduction = process.env.NODE_ENV === "production";

module.exports = {
    entry: {
        app:"./src/main.js"
    },
    output: {
        path: DIST_PATH,
        publicPath: PUBLIC,
        filename: "[name].[hash].js"
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: "vue-loader",
                options: {
                    extractCSS: true
                }
            },
            {
                test: /\.css$/,
                use: [
                    {loader: "style-loader"},
                    {loader: "css-loader"}
                ]
            },
            {
                test: /\.less$/,
                use: [
                    {loader: "style-loader"},
                    {loader: "css-loader"},
                    {loader: "less-loader"}
                ]
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                use: "file-loader"
            },
            {
                test: /\.(woff|woff2)$/,
                use: "url-loader?prefix=font/&limit=5000"
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                use: "url-loader?limit=10000&mimetype=application/octet-stream"
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                use: "url-loader?limit=10000&mimetype=image/svg+xml"
            },
            {
                test: /\.(png|jpg|gif)$/,
                loader: "file-loader",
                options: {
                    name: "[name].[ext]?[hash]"
                }
            }
        ]
    },
    resolve: {
        alias: {
            "vue$": "vue/dist/vue.esm.js"
        }
    },
    devServer: {
        historyApiFallback: true,
        overlay: true,
        proxy: {
            "/api": {
                //target: "http://122.225.88.132:20283",
                target: "http://localhost:8080",
                pathRewrite: {
                    "^/api": ""
                }
            }
        }
    },
    plugins: [
        // new webpack.DllReferencePlugin({
        //     context: __dirname,
        //     manifest: DIST_PATH + "/temp/vendors.manifest.json",
        // }),
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: "index.template.html",
            assetsPublic: isProduction ? PUBLIC : "/" + DIST + "/",
            assets: require(DIST_PATH + "/temp/vendors.assets.json")
        }),
        new ExtractTextPlugin("style.[contenthash].css")
    ],
    devtool: "cheap-source-map"
};

if (isProduction) {
    module.exports.devtool = false;
    // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "production")
        }),
        new CleanWebpackPlugin([DIST + "/app.*.js", DIST + "/style.*.css"]),
        new UglifyJSPlugin({
            cache: true,
            parallel: true,
            sourceMap: false, //default false
            uglifyOptions: {
                ecma: 8,
                warnings: false // default false
            }
        }),
        new BundleAnalyzerPlugin({
            analyzerMode: "static",
            reportFilename: "temp/app.report.html"
        })
    ])
}
